package logic

import (
	"context"
	"github.com/mojocn/base64Captcha"
	"github.com/zeromicro/go-zero/core/logx"
	"gooded/goodedrpc/goodedrpc"
	"gooded/goodedrpc/internal/svc"
	"gooded/model/redisinit"
	"time"
)

type CatpionLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCatpionLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CatpionLogic {
	return &CatpionLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CatpionLogic) Catpion(in *goodedrpc.CatpionRequest) (*goodedrpc.CatpionResponse, error) {
	// todo: add your logic here and delete this line
	c := base64Captcha.NewCaptcha(base64Captcha.DefaultDriverDigit, base64Captcha.DefaultMemStore)
	_, s, s2, err := c.Generate()
	if err != nil {
		return nil, err
	}
	redisinit.Client.Set("Catpion"+in.Username, s2, time.Minute*3)

	return &goodedrpc.CatpionResponse{
		Img: s,
	}, nil
}
